//
//  XMDynamicTableViewCell.m
//  XunMi
//
//  Created by MacBook on 2024/3/6.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMOtherDynamicTableViewCell.h"
#import "XMDynamicModel.h"
#import "XMCollectionFlowLayout.h"
#import "XMDynamicImgCollectionViewCell.h"
#import <YBImageBrowser/YBImageBrowser.h>
#import <YBImageBrowser/YBIBVideoData.h>


@interface XMOtherDynamicTableViewCell () <XMCollectionFlowLayoutDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) XMCollectionFlowLayout *layout;

@property (weak, nonatomic) IBOutlet UILabel *timeLa;
@property (weak, nonatomic) IBOutlet UILabel *contentLa;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentH;

@property (weak, nonatomic) IBOutlet UIView *imgBgV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgVH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *moreBtnH;
@property (weak, nonatomic) IBOutlet UILabel *omgCountLa;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentTopSpace;

@end

@implementation XMOtherDynamicTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.collectionView setCollectionViewLayout:self.layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMDynamicImgCollectionViewCell)) forCellWithReuseIdentifier:kStringFromClass(XMDynamicImgCollectionViewCell)];
    
}

- (void)setModel:(XMDynamicModel *)model {
    _model = model;
    self.moreBtn.selected = model.isOpening;
    self.moreBtn.hidden = !model.showMore;
    self.contentLa.text = model.content;
    self.contentH.constant = model.contentH;
    
    self.contentTopSpace.constant = [XMUtil isBlankString:model.content] ? 0 : 12;
    self.imgVH.constant = model.imageViewH;
    self.moreBtnH.constant = model.showMore ? 20 : 0;
    
    self.imgBgV.hidden = model.resource.count <= 0;
    self.omgCountLa.hidden = model.resource.count <= 3;
    
    [self.collectionView reloadData];
  
    self.timeLa.text = kStringFormat(@"%@——", model.release_time);
    
    self.omgCountLa.text = model.resource.count > 3 ? kStringFormat(@"+%ld", model.resource.count - 3) : @"";
    self.omgCountLa.hidden = model.resource.count > 3 ? NO : YES;
}


- (IBAction)moreAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(handelMore:cell:)]) {
        [self.delegate handelMore:self.model cell:self];
    }
}


#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.model.resource.count == 0) {
        return 0;
    }
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.model.resource.count == 0) {
        return 0;
    }
    if (self.model.resource.count < 4) {
        return self.model.resource.count;
    } else {
        return 3;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMDynamicImgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMDynamicImgCollectionViewCell) forIndexPath:indexPath];
    XMFileModel *model = self.model.resource[indexPath.item];
    
    if (model.file_type == XMFileTypeImage) {
        [cell.imgV sd_setImageWithURL:kStringToUrl(model.file_url) placeholderImage:kDefaultCoverImg];
        cell.playImgV.hidden = YES;
        if (indexPath.item == 2) {
            cell.coverView.hidden = NO;
        } else {
            cell.coverView.hidden = YES;
        }
    } else if (model.file_type == XMFileTypeImageVideo) {
        [cell.imgV sd_setImageWithURL:kStringToUrl(model.video_cover) placeholderImage:kDefaultCoverImg];
        cell.playImgV.hidden = NO;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XMDynamicImgCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    __block NSMutableArray *datas = [NSMutableArray array];
    [self.model.resource enumerateObjectsUsingBlock:^(XMFileModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 网络图片
        if (obj.file_type == XMFileTypeImage) {
            YBIBImageData *data = [YBIBImageData new];
            data.imageURL = [NSURL URLWithString:obj.file_url];
            data.projectiveView = cell.imgV;
            [datas addObject:data];
        }
        if (obj.file_type == XMFileTypeImageVideo) {
            YBIBVideoData *data = [YBIBVideoData new];
            data.videoURL = [NSURL URLWithString:obj.file_url];
            data.projectiveView = cell.imgV;
            [datas addObject:data];
        }
        
    }];
    
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = datas;
    browser.currentPage = indexPath.item;
    [browser show];
    
}

#pragma mark - XMCollectionFlowLayoutD
// 一行显示N个
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout numberOfColumnInSection:(NSInteger)section {
    if (self.model.resource.count < 4) {
        return self.model.resource.count;
    } else {
        return 3;
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout itemWidth:(CGFloat)width heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.imageViewH;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (XMCollectionFlowLayout *)layout {
    if (_layout == nil) {
        _layout = [[XMCollectionFlowLayout alloc] init];
        _layout.dataSource = self;
    }
    return _layout;
}

@end
